#include "MainHeader.h"
/*class cTSAEditorClass{
public:
     cTSAEditorClass();
	 ~cTSAEditorClass();
	 tBlock  CurrentBlock[4];
	 tTSA    blockset;
	
	 HWND TilesetPic;
	 HWND BlocksetPic;
	 HWND Preview;
	 HWND tMain;
	 Image Tileset;
	 Image iPreview;
	 Image TSA;
	 sChecks HorChk[4];
	 sChecks VerChk[4];
	 sCombo cboPals[4];
	 int Create();
	 int DrawThisTileset();
	 int DrawPreview();
	 int DrawTSA();
	 int LoadTSA();
	 int CreateTilesetWind();
	 int CreatePreviewWind();
	 int CreateTSAWind();
};   */
#include "TSAEditor.h"
BOOL CALLBACK TSAProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


	switch (message) 
	{
		case WM_INITDIALOG:
			MyTSAEditor.tMain = hWnd;
			MyTSAEditor.Create();
			break;
		case WM_LBUTTONDOWN:
			break;
		case WM_VSCROLL:
			break;
		case WM_RBUTTONDOWN:
	
			break;
				case WM_RBUTTONUP:
			
			break;
		case WM_MOUSEMOVE:
		
		
			break;

	
		case WM_PAINT:
			 
       

      
	
			break;
		case WM_SIZE:
            
			break;
		case WM_COMMAND:
			switch(LOWORD(wParam)){
				//Handle Palettes first
				if(HIWORD(wParam)==CBN_SELCHANGE){
			case cbopallettetsa:
			
				MyTSAEditor.CurrentBlock[0].Pal=(u16)MyTSAEditor.cboPals[0].GetListIndex();
				MyTSAEditor.DrawPreview();
				break;
			case cbopallettetsa2:
				MyTSAEditor.CurrentBlock[1].Pal=(u16)MyTSAEditor.cboPals[1].GetListIndex();
				MyTSAEditor.DrawPreview();
				break;
			case cbopallettetsa3:
				MyTSAEditor.CurrentBlock[2].Pal=(u16)MyTSAEditor.cboPals[2].GetListIndex();
				MyTSAEditor.DrawPreview();
				break;
			case cbopallettetsa4:
				MyTSAEditor.CurrentBlock[3].Pal=(u16)MyTSAEditor.cboPals[3].GetListIndex();
				MyTSAEditor.DrawPreview();
				break;
				}
			case vertchk:
				MyTSAEditor.CurrentBlock[0].VFlip=MyTSAEditor.VerChk[0].value()*0x800;
				MyTSAEditor.DrawPreview();
				break;
			case vertchk2:
				MyTSAEditor.CurrentBlock[1].VFlip=MyTSAEditor.VerChk[1].value()*0x800;
				MyTSAEditor.DrawPreview();
				break;
			case vertchk3:
				MyTSAEditor.CurrentBlock[2].VFlip=MyTSAEditor.VerChk[2].value()*0x800;
				MyTSAEditor.DrawPreview();
				break;
			case vertchk4:
				MyTSAEditor.CurrentBlock[3].VFlip=MyTSAEditor.VerChk[2].value()*0x800;
				MyTSAEditor.DrawPreview();
				break;
			case chkHor:
				MyTSAEditor.CurrentBlock[0].HFlip=MyTSAEditor.HorChk[0].value()*0x400;
				MyTSAEditor.DrawPreview();				
				break;
			case chkHor2:
				MyTSAEditor.CurrentBlock[1].HFlip=MyTSAEditor.HorChk[1].value()*0x400;
				MyTSAEditor.DrawPreview();				
				break;
			case chkHor3:
				MyTSAEditor.CurrentBlock[2].HFlip=MyTSAEditor.HorChk[2].value()*0x400;
				MyTSAEditor.DrawPreview();				
				break;
			case chkHor4:
				MyTSAEditor.CurrentBlock[3].HFlip=MyTSAEditor.HorChk[3].value()*0x400;
				MyTSAEditor.DrawPreview();				
				break;
			case cmdSaveBlock:
			    MyTSAEditor.SaveBlock();
				break;
            case cboTP:
				if(HIWORD(wParam)==CBN_SELCHANGE) MyTSAEditor.DrawThisTileset();
				break;
	     	case WM_DESTROY:
					ShowWindow(MyTSAEditor.tMain,SW_HIDE);
				break;
			}
			break;
		

	 }		
            
   return 0;
  
}

LRESULT CALLBACK BlockPreviewProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc=NULL;
	PAINTSTRUCT ps;
	int a=0;
	int b=0;

	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		MyTSAEditor.iPreview.Blit(hdc,0,0,16,16,0,0);
		EndPaint(hWnd, &ps);
		break;
		
  	case WM_LBUTTONDOWN:
	//	sprintf(buff,"Tile: %d",(GetX(lParam)+GetY(lParam))/8  );
	//	MessageBox(hWnd,buff,"None",MB_OK);
		a=GetX(lParam);
		b=GetY(lParam);
		MyTSAEditor.CurrentBlock[(a/8)+(b/8)*2].Tile=MyTSAEditor.curtsa;
		MyTSAEditor.DrawPreview();
			break;
   
	}
	
    return DefWindowProc(hWnd, message, wParam, lParam);
	
}


LRESULT CALLBACK BlockTilesetProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc=NULL;
	PAINTSTRUCT ps;
	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
	
		MyTSAEditor.Tileset.Blit(hdc,0,0,288,1024,0,0);
		EndPaint(hWnd, &ps);
		break;
		
  		case WM_LBUTTONDOWN:
		MyTSAEditor.curtsa= (GetX(lParam)/8)+(GetY(lParam)/8)*16;
		
			break;
   
	}
	
    return DefWindowProc(hWnd, message, wParam, lParam);
	
}

cTSAEditorClass::cTSAEditorClass(){

};
cTSAEditorClass::~cTSAEditorClass(){

};

int cTSAEditorClass::Create(){
	int i= 0;
	HWND hrkwnd=NULL;
	HWND verwnd=NULL;
	HWND palwnd=NULL;
	char buf[256]={0};
      CreateTilesetWind();
	  CreatePreviewWind();
	 
       //Load up controls
	 curtsa=0;
	   for(i=0;i<4;i++){
		   switch(i){

		   case 0:
               hrkwnd=GetDlgItem(tMain,chkHor);
			   verwnd=GetDlgItem(tMain,vertchk);
			   palwnd=GetDlgItem(tMain,cbopallettetsa);
			   break;
		   case 1:
               hrkwnd=GetDlgItem(tMain,chkHor2);
			   verwnd=GetDlgItem(tMain,vertchk2);
			   palwnd=GetDlgItem(tMain,cbopallettetsa2);
			   break;
		   case 2:
               hrkwnd=GetDlgItem(tMain,chkHor3);
			   verwnd=GetDlgItem(tMain,vertchk3);
			   palwnd=GetDlgItem(tMain,cbopallettetsa3);
			   break;
		   case 3:
               hrkwnd=GetDlgItem(tMain,chkHor4);
			   verwnd=GetDlgItem(tMain,vertchk4);
			   palwnd=GetDlgItem(tMain,cbopallettetsa4);
			   break;
		   }
		    HorChk[i].SetCnt(hrkwnd);
            VerChk[i].SetCnt(verwnd);
            cboPals[i].Init(palwnd);
		    
		
	   }
	   cboTSAPAL.Init(GetDlgItem(tMain,cboTP));
	   cboTSAPAL.Clear();
			for(i=0;i<4;i++){
				for(int p=0;p<0xF;p++){
					sprintf(buf,"%X",p);
					cboPals[i].Additem(buf);
					if(!i) cboTSAPAL.Additem(buf);
				}
				cboPals[i].SetListIndex(0);
				if(!i)cboTSAPAL.Additem(buf);
			}


	        Tileset.Create(512,1024);
	        iPreview.Create(32,32);
	        cboTSAPAL.SetListIndex(2);
		
			DrawThisTileset();
			
			curtsa=0;
			LoadTSA();
			
	return 0;
}

int cTSAEditorClass::CreateTilesetWind(){
   WNDCLASSEX blar8;
   RECT t;
   HWND p=GetDlgItem(tMain,fraTTileset);
	memset(&blar8,0,sizeof(blar8));
	blar8.cbSize = sizeof(blar8);		   // size of structure 
	blar8.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	blar8.lpfnWndProc = &BlockTilesetProc;	  // points to window procedure 
	blar8.cbClsExtra = NULL;				// no extra class memory 
	blar8.cbWndExtra =NULL; 			   // no extra window memory 
	blar8.hInstance = hGlobal;		   // handle to instance 
	blar8.hIcon = NULL; 			 // predefined app. icon 
	blar8.hCursor = NULL;				// predefined arrow 
	//blar8.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH);				   // white background brush 
	blar8.lpszMenuName =  NULL;    // name of menu resource 
	blar8.lpszClassName = "TSATilesetWind";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&blar8);
	GetWindowRect(p,&t);
	 TilesetPic = CreateWindowEx(NULL,"TSATilesetWind",NULL, WS_BORDER|WS_TABSTOP | WS_VISIBLE|WS_CHILD ,0 ,0 ,t.right-t.left ,670,p,0,hGlobal,0);
	
	ShowWindow(TilesetPic,SW_SHOW);
	return 0;
}

int cTSAEditorClass::CreatePreviewWind(){
   WNDCLASSEX ban9;
   
//   HWND p=GetDlgItem(tMain,fraPreview);
	memset(&ban9,0,sizeof(ban9));
	ban9.cbSize = sizeof(ban9);		   // size of structure 
	ban9.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	ban9.lpfnWndProc = &BlockPreviewProc;	  // points to window procedure 
	ban9.cbClsExtra = NULL;				// no extra class memory 
	ban9.cbWndExtra =NULL; 			   // no extra window memory 
	ban9.hInstance = hGlobal;		   // handle to instance 
	ban9.hIcon = NULL; 			 // predefined app. icon 
	ban9.hCursor = NULL;				// predefined arrow 
	//ban9.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH);				   // white background brush 
	ban9.lpszMenuName =  NULL;    // name of menu resource 
	ban9.lpszClassName = "TSAPreviewWind";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&ban9);

	Preview= CreateWindowEx(NULL,"TSAPreviewWind",NULL, WS_TABSTOP | WS_VISIBLE|WS_CHILD ,175 ,32 ,32 ,32 ,	MyTSAEditor.tMain ,0,hGlobal,0);
	
	
	ShowWindow(Preview,SW_SHOW);
	return 0;
}




int cTSAEditorClass::DrawThisTileset(){
	int i =0;
		Tileset.SetPalette(GameEngine->Members.PcPalMem);
		Tileset.Clear();
	for(i=0;i<1024;i++){
      Tileset.Draw(TileImage,(i % 16)*8, (i/16)*8, (cboTSAPAL.GetListIndex()<<12)+i);
	}
	InvalidateRect(TilesetPic,0,1);
	return 0;
}




int cTSAEditorClass::LoadTSA(){
int i =0;
	for(i=0;i<4;i++){
     	CurrentBlock[i].Pal   = (GameEngine->Members.TSA.nTSA[Tile*4+i] /0x1000);
        CurrentBlock[i].HFlip = (GameEngine->Members.TSA.nTSA[Tile*4+i] & 0x400);
		CurrentBlock[i].VFlip = (GameEngine->Members.TSA.nTSA[Tile*4+i] & 0x800);
		CurrentBlock[i].Tile  = (GameEngine->Members.TSA.nTSA[Tile*4+i] & 0x3FF);
	}

	  for(i=0;i<4;i++){
		 
            
			 HorChk[i].value( CurrentBlock[i].HFlip/0x400 );
            VerChk[i].value( CurrentBlock[i].VFlip/0x800 );
            cboPals[i].SetListIndex( CurrentBlock[i].Pal  );
		
			 
		  
		   }
	 
         DrawPreview();
	return 0;
}

int cTSAEditorClass::SaveBlock(){
//Will code after loading is done correctly.
	u16 block;
	RHeader* RoomHeader=GameEngine->Members.RoomHeader;
	u32 offset =  GameEngine->Members.RoomTilesets[RoomHeader->bTileset].TSAMap-0x8000000;
    
	for(int i=0;i<4;i++){
    block =  CurrentBlock[i].Pal*0x1000  
		    +CurrentBlock[i].HFlip
			+CurrentBlock[i].VFlip 
			+CurrentBlock[i].Tile;

     GameEngine->Members.TSA.nTSA[Tile*4+i]=block;
	}
	//fseek(GBA.ROM,offset,SEEK_SET);	
	//fwrite(&TSA.ID,sizeof(short),1,GBA.ROM);   
	//fwrite(&TSA.nTSA,sizeof(short),0x1080,GBA.ROM);
    u32 tileoffset=offset+2+Tile*8;
	fseek(GBA.ROM,tileoffset,SEEK_SET);
	fwrite(&GameEngine->Members.TSA.nTSA[Tile*4],2,4,GBA.ROM);
    fclose(GBA.ROM);
	GBA.ROM = fopen(GBA.FileLoc,"r+b");
	GetTileset(RoomHeader->bTileset);
	return 0;
}

int cTSAEditorClass::DrawPreview(){
	   iPreview.SetPalette(GameEngine->Members.PcPalMem);
	   iPreview.Clear();
		iPreview.Draw(TileImage,0,0,CurrentBlock[0].Pal*0x1000
								   +CurrentBlock[0].HFlip 
								   +CurrentBlock[0].VFlip 
								   +CurrentBlock[0].Tile);
		iPreview.Draw(TileImage,8,0,CurrentBlock[1].Pal*0x1000
								   +CurrentBlock[1].HFlip 
								   +CurrentBlock[1].VFlip 
								   +CurrentBlock[1].Tile);
		iPreview.Draw(TileImage,0,8,CurrentBlock[2].Pal*0x1000
								   +CurrentBlock[2].HFlip 
								   +CurrentBlock[2].VFlip 
								   +CurrentBlock[2].Tile);
		iPreview.Draw(TileImage,8,8,CurrentBlock[3].Pal*0x1000
								   +CurrentBlock[3].HFlip 
								   +CurrentBlock[3].VFlip 
								   +CurrentBlock[3].Tile);
		InvalidateRect(Preview,0,1);
	return 0;
}